#define PT 0x03BE
#define PLBA 8
#define DLBA 8
#define DEST 4

# we are using %cs as a segment descr

.code16
.text

.global _start
_start:
	jmp		main

.word 0xFADA #magical number

msg_buf2:
.ascii "Wybierz partycje do zaladowania.\n\r"
.byte 0

berr_msg:
.ascii "Partycja nie-bootowalna.\n\r"
.byte 0

perr_msg:
.ascii "Zly numer partycji (1-4).\n\r"
.byte 0

main:
	movw    %cs, %ax
	# Location of data is DS:Offset
	movw    %ax, %ds	# This can not be loaded directly it has to be in two steps.
	movw    %ax, %ss
	cld	    # lodsb will increment %si

	movw    $msg_buf2, %si # Load the string into position for the procedure.
	call	write_msg

	movw    $0x01c0, %si # Load the string into position for the procedure.
	call	write_msg

	movw    $0x01d0, %si # Load the string into position for the procedure.
	call	write_msg

	movw    $0x01e0, %si # Load the string into position for the procedure.
	call	write_msg

	movw    $0x01f0, %si # Load the string into position for the procedure.
	call	write_msg

	#wczytaj MBR
	mov	    $0x42, %ah
	mov	    $0x80, %dl
	movw    $dap, %si
	int	    $0x13
	jc	 	die2

	xorl   	%eax, %eax
	int	    $0x16

	andl	$0x0FF, %eax

	cmp		$48, %ax
	je		laduj_mbr

	subl	$49, %eax

	cmp	$4, %ax
	jge	perr

	shl		$4, %eax
	addl	$PT, %eax
	addl	$PLBA, %eax
	movl	(%eax), %ecx

	jmp		load

laduj_mbr:
	movl	$0x0, %ecx

load:
	#sektor do zaladowania
	xorl	%ebx, %ebx
	movl	$(dap+DEST), %ebx
	movl	$0x7c00, (%ebx)
	movl	$(dap+DLBA), %ebx
	movl	%ecx, (%ebx)

	#czytanie sektora
	mov	    $0x42, %ah
	mov	    $0x80, %dl
	movw    $dap, %si
	int	    $0x13
	jc	 	die2

	#ladujemy

	#sprawdz czy bootsector 0xaa55
	movw	$0x0, %ax
	movw    %ax, %ds
	movw	(0x7dfe), %ax
	xorw	$0xaa55, %ax

	jz	skocz

	movw	%cs, %ax
	movw    %ax, %ds
	movw	$berr_msg, %si # Load the string into position for the procedure.
	call	write_msg
	jmp 	main

skocz:
	ljmp	$0x0, $0x7c00

die2:
	int		$0x18

perr:
	movw	$perr_msg, %si
	call	write_msg
	jmp		main

#procedura

write_msg:
    mov	    $0x0E, %ah	# The function to display a chacter (teletype)
    movw    $0x07, %bx	# Normal text attribute

nextchar:	# Internal label (needed to loop round for the next character)
    lodsb	# I think of this as LOaD String Block 
		# (Not sure if thats the real meaning though)
		# Loads [SI] into AL and increases SI by one

    # Check for end of string '0' 
    or	    %al, %al # Sets the zero flag if al = 0 
		    # (OR outputs 0s where there is a zero bit in the register)
    jz	    die	    # If the zero flag has been set go to the end of the procedure.
		    # Zero flag gets set when an instruction returns 0 as the answer.
    int	    $0x10   # Run the BIOS video interrupt 
    jmp	    nextchar    # Loop back round tothe top

die:	    # Label at the end to jump to when complete
    ret


dap:
.byte 0x10  #size of DAP (Disk Address Packet)
.byte 0	    # magic number
.byte 1	    #number of sectors to read
.byte 0	    # magic number
.long 0x00008200    #segment:offest to the memory, hardcoded
.long 0x00000000    #LSB of first sector to read (indexed from 0)
.long 0x00000000    #MSB of sector to read (indexed from 0)

